package com.fymxy.question_bank_management.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fymxy.question_bank_management.pojo.*;
import com.fymxy.question_bank_management.service.Paper2ProService;
import com.fymxy.question_bank_management.service.PaperService;
import com.fymxy.question_bank_management.service.ProblemService;
import com.fymxy.question_bank_management.util.ApiResultHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.lang.reflect.Array;
import java.util.*;

@RestController

public class PaperController {

    @Autowired
    private PaperService paperService;
    @Autowired
    private Paper2ProService paper2ProService;
    @Autowired
    private ProblemService problemService;

    @PostMapping("/addExams")
    public ApiResult autoGeneratePaper(@RequestBody ReceiveTest options){

        //解析请求参数
        String level = options.getExamLevel();
        String paperName = options.getExamName();
        List<String> keywords = options.getKeywords();
        String name=options.getExamName();
        Integer paperSetterNo = options.getTeacherNo();

        //试卷自动组题
        int paperNo = paperService.getTopPaperNo()+1;
        int res = paperService.autoGeneratePaper(level,keywords,paperNo,name,paperSetterNo);

        return ApiResultHandler.buildApiResult(200,"请求成功",res);
    }

    @GetMapping("/exam/{examCode}")
    public ApiResult getExam(@PathVariable("examCode") int examCode){
        List<Integer> proNo = this.paper2ProService.selectByPaperNo(examCode);
        Paper paper = this.paperService.selectByPaperNo(examCode);
        List<Problem> problemList = new ArrayList<>();
        for(int pro:proNo){
            problemList.add(this.problemService.selectByProblemNo(pro));
        }


        Collections.sort(problemList, new Comparator<Problem>() {
            @Override
            public int compare(Problem o1, Problem o2) {
                if (o1.getProblemType().equals("choice"))return -1;
                else if (o1.getProblemType().equals("fill") && o2.getProblemType().equals("cal"))return -1;
                return 1;
            }
        });

        result result2 = new result(problemList, paper);
        return ApiResultHandler.buildApiResult(200,"试卷和题目",result2);
    }

    

    @ResponseBody
    private class result{
        private  List<Problem> problemList;
        private  Paper paper;

        public List<Problem> getProblemList() {
            return problemList;
        }

        public Paper getPaper() {
            return paper;
        }

        public void setProblemList(List<Problem> problemList) {
            this.problemList = problemList;
        }

        public void setPaper(Paper paper) {
            this.paper = paper;
        }

        public result(List<Problem> problemList, Paper paper) {
            this.problemList = problemList;
            this.paper = paper;
        }
    }

}
